package com.hjq.mallorder.listener;

import com.hjq.mallorder.entity.OrderEntity;
import com.hjq.mallorder.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @auther HJQ
 */
@RabbitListener(queues ="order.release.order.queue")
@Service
public class OrderCloseListener {
    @Autowired
    OrderService orderService;
    @RabbitHandler
    public  void orderService(OrderEntity orderEntity, Channel channel, Message msg)  {
        try {
            orderService.closeOrder(orderEntity);
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
            System.out.println("订单关了"+orderEntity.getOrderSn()+"==>"+orderEntity.getId().toString());
        } catch (IOException e) {
            e.printStackTrace();
            try {
                channel.basicReject(msg.getMessageProperties().getDeliveryTag(),true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }


    }
}
